<template>
  <el-dialog
     v-drag
    :title="!dataForm.detailId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :inline="true" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<el-form-item label="盘亏编号" prop="lossNo">
  <el-input size="mini"  v-model="dataForm.lossNo" placeholder="盘亏编号" clearable></el-input>
</el-form-item>
<el-form-item label="盘亏id" prop="lossId">
  <el-input size="mini"  v-model="dataForm.lossId" placeholder="盘亏id" clearable></el-input>
</el-form-item>
<el-form-item label="产品id" prop="productId">
  <el-input size="mini"  v-model="dataForm.productId" placeholder="产品id" clearable></el-input>
</el-form-item>
<el-form-item label="产品编号" prop="productCode">
  <el-input size="mini"  v-model="dataForm.productCode" placeholder="产品编号" clearable></el-input>
</el-form-item>
<el-form-item label="产品名称" prop="productName">
  <el-input size="mini"  v-model="dataForm.productName" placeholder="产品名称" clearable></el-input>
</el-form-item>
<el-form-item label="条形码" prop="barCode">
  <el-input size="mini"  v-model="dataForm.barCode" placeholder="条形码" clearable></el-input>
</el-form-item>
<el-form-item label="通用名称" prop="genericName">
  <el-input size="mini"  v-model="dataForm.genericName" placeholder="通用名称" clearable></el-input>
</el-form-item>
<el-form-item label="规格" prop="standard">
  <el-input size="mini"  v-model="dataForm.standard" placeholder="规格" clearable></el-input>
</el-form-item>
<el-form-item label="单位" prop="unit">
  <el-input size="mini"  v-model="dataForm.unit" placeholder="单位" clearable></el-input>
</el-form-item>
<el-form-item label="批准文号" prop="approvalNo">
  <el-input size="mini"  v-model="dataForm.approvalNo" placeholder="批准文号" clearable></el-input>
</el-form-item>
<el-form-item label="产地" prop="producingArea">
  <el-input size="mini"  v-model="dataForm.producingArea" placeholder="产地" clearable></el-input>
</el-form-item>
<el-form-item label="厂家" prop="produceFactory">
  <el-input size="mini"  v-model="dataForm.produceFactory" placeholder="厂家" clearable></el-input>
</el-form-item>
<el-form-item label="剂型" prop="dosageForm">
  <el-input size="mini"  v-model="dataForm.dosageForm" placeholder="剂型" clearable></el-input>
</el-form-item>
<el-form-item label="有效期" prop="expiryDate">
  <el-input size="mini"  v-model="dataForm.expiryDate" placeholder="有效期" clearable></el-input>
</el-form-item>
<el-form-item label="生产日期" prop="produceDate">
  <el-input size="mini"  v-model="dataForm.produceDate" placeholder="生产日期" clearable></el-input>
</el-form-item>
<el-form-item label="批号" prop="batchNo">
  <el-input size="mini"  v-model="dataForm.batchNo" placeholder="批号" clearable></el-input>
</el-form-item>
<el-form-item label="批次号" prop="lotNo">
  <el-input size="mini"  v-model="dataForm.lotNo" placeholder="批次号" clearable></el-input>
</el-form-item>
<el-form-item label="售价" prop="tradePrice">
  <el-input size="mini"  v-model="dataForm.tradePrice" placeholder="售价" clearable></el-input>
</el-form-item>
<el-form-item label="盘亏金额" prop="lossFee">
  <el-input size="mini"  v-model="dataForm.lossFee" placeholder="盘亏金额" clearable></el-input>
</el-form-item>
<el-form-item label="盘亏数量" prop="lossNum">
  <el-input size="mini"  v-model="dataForm.lossNum" placeholder="盘亏数量" clearable></el-input>
</el-form-item>
<el-form-item label="门店id" prop="storeId">
  <el-input size="mini"  v-model="dataForm.storeId" placeholder="门店id" clearable></el-input>
</el-form-item>
<el-form-item label="门店名称" prop="storeName">
  <el-input size="mini"  v-model="dataForm.storeName" placeholder="门店名称" clearable></el-input>
</el-form-item>
<el-form-item label="门店编码" prop="storeCode">
  <el-input size="mini"  v-model="dataForm.storeCode" placeholder="门店编码" clearable></el-input>
</el-form-item>
<el-form-item label="创建时间" prop="createTime">
  <el-input size="mini"  v-model="dataForm.createTime" placeholder="创建时间" clearable></el-input>
</el-form-item>
<el-form-item label="修改时间" prop="updateTime">
  <el-input size="mini"  v-model="dataForm.updateTime" placeholder="修改时间" clearable></el-input>
</el-form-item>
<el-form-item label="创建人" prop="createUsername">
  <el-input size="mini"  v-model="dataForm.createUsername" placeholder="创建人" clearable></el-input>
</el-form-item>
<el-form-item label="创建人id" prop="createUserId">
  <el-input size="mini"  v-model="dataForm.createUserId" placeholder="创建人id" clearable></el-input>
</el-form-item>
<el-form-item label="修改人" prop="updateUsername">
  <el-input size="mini"  v-model="dataForm.updateUsername" placeholder="修改人" clearable></el-input>
</el-form-item>
<el-form-item label="修改人id" prop="updateUserId">
  <el-input size="mini"  v-model="dataForm.updateUserId" placeholder="修改人id" clearable></el-input>
</el-form-item>
<el-form-item label="是否可用 1:是" prop="status">
  <el-input size="mini"  v-model="dataForm.status" placeholder="是否可用 1:是" clearable></el-input>
</el-form-item>
<el-form-item label="版本" prop="version">
  <el-input size="mini"  v-model="dataForm.version" placeholder="版本" clearable></el-input>
</el-form-item>
<el-form-item label="是否删除 1:是" prop="isDeleted">
  <el-input size="mini"  v-model="dataForm.isDeleted" placeholder="是否删除 1:是" clearable></el-input>
</el-form-item>
<el-form-item label="备注" prop="remark">
  <el-input size="mini"  v-model="dataForm.remark" placeholder="备注" clearable></el-input>
</el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          detailId: 0,
          lossNo: '',
          lossId: '',
          productId: '',
          productCode: '',
          productName: '',
          barCode: '',
          genericName: '',
          standard: '',
          unit: '',
          approvalNo: '',
          producingArea: '',
          produceFactory: '',
          dosageForm: '',
          expiryDate: '',
          produceDate: '',
          batchNo: '',
          lotNo: '',
          tradePrice: '',
          lossFee: '',
          lossNum: '',
          storeId: '',
          storeName: '',
          storeCode: '',
          createTime: '',
          updateTime: '',
          createUsername: '',
          createUserId: '',
          updateUsername: '',
          updateUserId: '',
          status: '',
          version: '',
          isDeleted: '',
          remark: ''
        },
        dataRule: {
          lossNo: [
            { required: true, message: '盘亏编号不能为空', trigger: 'blur' }
          ],
          lossId: [
            { required: true, message: '盘亏id不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '产品id不能为空', trigger: 'blur' }
          ],
          productCode: [
            { required: true, message: '产品编号不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' }
          ],
          barCode: [
            { required: true, message: '条形码不能为空', trigger: 'blur' }
          ],
          genericName: [
            { required: true, message: '通用名称不能为空', trigger: 'blur' }
          ],
          standard: [
            { required: true, message: '规格不能为空', trigger: 'blur' }
          ],
          unit: [
            { required: true, message: '单位不能为空', trigger: 'blur' }
          ],
          approvalNo: [
            { required: true, message: '批准文号不能为空', trigger: 'blur' }
          ],
          producingArea: [
            { required: true, message: '产地不能为空', trigger: 'blur' }
          ],
          produceFactory: [
            { required: true, message: '厂家不能为空', trigger: 'blur' }
          ],
          dosageForm: [
            { required: true, message: '剂型不能为空', trigger: 'blur' }
          ],
          expiryDate: [
            { required: true, message: '有效期不能为空', trigger: 'blur' }
          ],
          produceDate: [
            { required: true, message: '生产日期不能为空', trigger: 'blur' }
          ],
          batchNo: [
            { required: true, message: '批号不能为空', trigger: 'blur' }
          ],
          lotNo: [
            { required: true, message: '批次号不能为空', trigger: 'blur' }
          ],
          tradePrice: [
            { required: true, message: '售价不能为空', trigger: 'blur' }
          ],
          lossFee: [
            { required: true, message: '盘亏金额不能为空', trigger: 'blur' }
          ],
          lossNum: [
            { required: true, message: '盘亏数量不能为空', trigger: 'blur' }
          ],
          storeId: [
            { required: true, message: '门店id不能为空', trigger: 'blur' }
          ],
          storeName: [
            { required: true, message: '门店名称不能为空', trigger: 'blur' }
          ],
          storeCode: [
            { required: true, message: '门店编码不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          createUsername: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          updateUsername: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          updateUserId: [
            { required: true, message: '修改人id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '是否可用 1:是不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除 1:是不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.detailId = id || 0
        this.visible = true
        this.$nextTick(async () => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.detailId) {
            const res = await this.$http({
              url: `/stock/stocklossdetail/info/${this.dataForm.detailId}`,
              method: 'get',
              params: this.$http.adornParams()
            })
                let data = res.data
                if (data && data.code === 0) {
                    this.dataForm = data.stockLossDetail
                }
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate(async (valid) => {
          if (valid) {
            const res = await this.$http({
              url:`/stock/stocklossdetail/${!this.dataForm.detailId ? 'save' : 'update'}`,
              method: 'post',
             data: this.$http.adornData( this.dataForm)
            })
            let data = res.data
            if (data && data.code === 0) {
                this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                }
            })
            } else {
                this.$message.error(data.msg)
            }
          }
        })
      }
    }
  }
</script>
